<template>
  <div class='container'>
    <el-card >
      <!------------------------- 面包屑-------------------- -->
      <el-breadcrumb separator-class="el-icon-arrow-right" v-if="subjectID" class="el-breadcrumb">
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{$route.query.name}}</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>
        <!-- <hr> -->
      <!----------------------------- 头部--------------------------- -->
    <el-form label-width="100px" :inline="true" :model="tagsForm">
        <el-form-item label="标签名称">
           <el-input style="width:200px;margin-right:10px" size="small" v-model="tagsForm.tagName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select placeholder="请选择" v-model="tagsForm.state">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
          <el-button size="small" style="margin-left:10px" @click="emptyIpt">清除</el-button>
          <el-button  size="small" type="primary" @click="filterTags">搜索</el-button>
        </el-form-item>
        <!-- margin-left:300px -->
        <el-form-item style="float:right">
            <el-button size="small" type="text" icon="el-icon-back" @click="$router.push('/subjects/list')">返回学科</el-button>
            <el-button size="small" type="success" icon="el-icon-edit" @click="showDialog=true">新增标签</el-button>
        </el-form-item>
   </el-form>

  <!------------------------ 警告框 --------------------->
    <el-alert
      :title=" '数据一共' + counts + '条'"
      type="info"
      :closable="false"
      show-icon>
     </el-alert>

     <!-- -----------------表格区域 ---------------------->
     <el-table v-loading="loading" :data="tagsList" style="width: 100%">
          <el-table-column type="index" :index="indexMethod" label="序号" width="100"></el-table-column>
          <el-table-column prop="subjectName" label="所属学科"  width="180"></el-table-column>
          <el-table-column prop="tagName" label="标签名称"> </el-table-column>
          <el-table-column prop="username" label="创建者">  </el-table-column>

          <el-table-column  label="创建日期">
            <template #default="{row}">
                {{row.addDate | formatDateSub}}
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="state" :formatter="formatState"></el-table-column>
          <el-table-column  prop="address" label="操作">
              <template #default="{row}">

                <el-button type="text" @click="changeStatus(row)">

                  {{row.state === 1 ? '禁用' :  '启用'}}
                </el-button>
                <el-button type="text" :disabled="row.state ===1" @click="editTag(row)">修改 </el-button>
                <el-button type="text" :disabled="row.state ===1" @click="delTag(row)">删除</el-button>
           </template>
          </el-table-column>
    </el-table>
    <!-- -----------------------弹框-------------------- -->
    <TagsAdd
        @handleCloseDialog="handleCloseDialog"
        :showDialog="showDialog"
        :routerId="subjectID"
        @addTags="addTags"
        @editSuccess="editSuccess"
        ref="tagAdd"
        ></TagsAdd>
    <!-- --------------------------分页-------------------- -->
     <div class="pagination">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[2, 4, 6, 8,10,12,14]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts">
    </el-pagination>
      </div>
</el-card>

  </div>
</template>

<script>
import { reqGetlist, remove, detail, changeState } from '@/api/hmmm/tags'
import { status } from '@/api/hmmm/constants'
import TagsAdd from '@/module-hmmm/components/tags-add.vue'
export default {
  name: 'Tags',
  components: { TagsAdd },
  data () {
    return {
      showDialog: false,
      loading: false,
      tagsList: [],
      page: 1,
      pagesize: 10,
      pages: 1,
      counts: 1,
      tagsForm: {
        tagName: null,
        state: null,
        subjectID: this.$route.query.id
      }
    }
  },
  created () {
    this.getTagsList()
  },
  computed: {
    subjectID () {
      return this.$route.query.id
    }
  },
  methods: {
    // 获取标签列表
    async getTagsList () {
      const obj = {
        page: this.page,
        pagesize: this.pagesize,
        ...this.tagsForm
      }
      this.loading = true
      const res = await reqGetlist(obj)
      this.tagsList = res.data.items
      this.page = +res.data.page
      this.pagesize = +res.data.pagesize
      this.pages = res.data.pages
      this.counts = res.data.counts
      this.loading = false
    },
    handleSizeChange (val) {
      this.pagesize = val
      this.page = 1
      this.getTagsList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getTagsList()
    },
    // 序号
    indexMethod (index) {
      return index + 1 + this.pagesize * (this.page - 1)
    },

    // 格式化状态
    formatState (row, column, cellValue, index) {
      const temp = status.find(item => item.value === +cellValue)
      return temp ? temp.label : '未知'
    },
    // 清除功能
    emptyIpt () {
      this.tagsForm = {}
    },
    // 搜索(筛选功能)
    async filterTags () {
      this.getTagsList()
    },

    handleCloseDialog () {
      this.showDialog = false
    },
    addTags () {
      this.getTagsList()
    },

    // 删除标签
    delTag (row) {
      this.$confirm('此操作将永久删除该标签,是否继续?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(row)
        if (res.status === 200) {
          this.getTagsList()
          this.$message.success('删除标签成功')
        }
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 按钮的启用和禁用状态
    async changeStatus (row) {
      console.log(row.state)
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState({
        id: row.id,
        state: row.state
      })
      console.log(row.state)
    },
    // 修改标签
    async editTag (row) {
      this.$refs.tagAdd.loading = true
      this.showDialog = true
      const res = await detail(row)
      console.log(res.data)
      this.$refs.tagAdd.formRule = res.data // 调用子组件属性
      this.$refs.tagAdd.loading = false
    },
    editSuccess () {
      this.getTagsList()
    }

  }

}
</script>

<style scoped lang='scss'>
    .container{
        .el-breadcrumb {
          margin-bottom: 20px;
        }

      .pagination{
          float: right;
        }
    }
</style>
